package com.sunnada.img;

import net.coobird.thumbnailator.Thumbnailator;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.junit.jupiter.api.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 使用Thumbnailtor工具进行压缩变换
 */
public class GoogleImage {

    String imgPath = "D:\\ProgramData\\Gitee Pro\\poiextended\\src\\main\\resources\\images\\01.jpg";
//    String imgPath2 = "D:\\ProgramData\\Gitee Pro\\poiextended\\src\\main\\resources\\images\\test.jpg";
//    String imgPath3 = "D:\\ProgramData\\Gitee Pro\\poiextended\\src\\main\\resources\\images\\";
//    String mark = "D:\\ProgramData\\Gitee Pro\\poiextended\\src\\main\\resources\\images\\timg.jpg";
@Test
    public void test(){
    try {
        InputStream f = new FileInputStream(imgPath);
        byte[] b = new byte[f.available()];
        int read = f.read(b);
//        byte[] compressImageByte = GoogleImage.getCompressImageByte(b, 100, 100);
        byte[] compressImageByte = GoogleImage.getCompressImageByte(b, 1);
        FileOutputStream o = new FileOutputStream(new File("11111111111112.jpg"));
        o.write(compressImageByte);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //this.rotate(imgPath,imgPath2,,0f);
//    this.t(imgPath,imgPath2,200,100);
//    try {
        //指定大小压缩
        //specifySize(imgPath,imgPath3+"size.jpg",200,200,true);
        //按比例缩放
        //specifyScale(imgPath,imgPath3+"scale.jpg",0.5f);
        //旋转图片
        //rotate(imgPath,imgPath3+"rotate.jpg",90);//err
        //水印图

        //specifySize(mark,mark,100,100,true);
        //waterMarking(imgPath,imgPath3+"mark.jpg",Positions.BOTTOM_CENTER,mark,400,400,0.2f,0.5f);
        //剪切图片
//        cut(imgPath,imgPath3+"cut.jpg",Positions.CENTER,400,400,100,100,true);
//        transformImageFormat(imgPath,imgPath3+"format.png","png");
        //toOutputstream(imgPath,imgPath3+"outpu.jpg");
//    } catch (IOException e) {
//        e.printStackTrace();
//    }

}

    /**
     * 指定大小压缩到目标路径
     * 以缩放比大的为准（原值/指定值）
     * @param imgSourcePath 源图片路径
     * @param imgGoalPath 目标图片路径
     * @param width 指定的图片宽度
     * @param height 指定的图片高度
     * @param scale 保持纵横比（true）
     */
    public static void specifySize(String imgSourcePath, String imgGoalPath, int width, int height,boolean scale) {
        try {
            Thumbnails.of(imgSourcePath).keepAspectRatio(scale).size(width,height).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按比例压缩图片
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param scale 缩放值（0-1）
     * @
     */
    public static void specifyScale(String imgSourcePath, String imgGoalPath, float scale){
        try {
            Thumbnails.of(imgSourcePath).scale(scale).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 旋转图片（按原尺寸）
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 目标目标路径
     * @param rotate 指定图片选择的角度（如：90 为旋转90度）
     * @
     */
    public static void rotateImage(String imgSourcePath, String imgGoalPath, float rotate){
        try {
            BufferedImage img = ImageIO.read(new FileInputStream(imgSourcePath));
            rotateImage(imgSourcePath,imgGoalPath,img.getWidth(),img.getHeight(),rotate);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 旋转图片并压缩
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param width 指定图片的宽
     * @param height 指定图片的高
     * @param rotate 指定图片选择的角度（如：90 为旋转90度）
     * @
     */
    public static void rotateImage(String imgSourcePath, String imgGoalPath,int width,int height, float rotate){
        try {
            Thumbnails.of(imgSourcePath).rotate(rotate).size(width,height).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 添加水印
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param markPosition 水印放置位置
     * @param waterMarkPath 水印图片路径
     * @param width 指定图片的宽
     * @param height 指定图片的高
     * @param markOpacity 水印透明度
     * @param markQualify 水印压缩质量
     * @
     */
    public static void addWaterMark(String imgSourcePath, String imgGoalPath,Positions markPosition,String waterMarkPath,int width,int height, float markOpacity,float markQualify){
        try {
            Thumbnails.of(imgSourcePath).watermark(markPosition, ImageIO.read(new File(waterMarkPath)),markOpacity).outputQuality(markQualify).size(width,height).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 剪切图片
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param positions 剪切位置
     * @param cutWidth 剪切的宽
     * @param cutHeight 剪切的高
     * @param width 压缩后的宽
     * @param height 压缩后的高
     * @param scale 保持缩放比（true）
     * @
     */
    public static void cutImage(String imgSourcePath, String imgGoalPath, Positions positions,int cutWidth,int cutHeight,int width ,int height,boolean scale){
        try {
            Thumbnails.of(imgSourcePath).sourceRegion(positions,cutWidth,cutHeight).keepAspectRatio(false).size(width,height).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片格式转换（按图片源尺寸）
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param format 图片格式（后缀）
     * @
     */
    public static void setImageFormat(String imgSourcePath, String imgGoalPath,String format){
        try {
            BufferedImage img = ImageIO.read(new FileInputStream(imgSourcePath));
            Thumbnails.of(imgSourcePath).outputFormat(format).size(img.getWidth(),img.getHeight()).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片格式转换（指定尺寸）
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param width 指定图片的宽
     * @param height 指定图片的高
     * @param format 图片格式（后缀）
     * @
     */
    public static void setImageFormat(String imgSourcePath, String imgGoalPath,int width,int height,String format){
        try {
            Thumbnails.of(imgSourcePath).outputFormat(format).size(width,height).toFile(imgGoalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 输出到OutputStream
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @return
     * @
     */
    public static OutputStream getCompressImageOutputStream(String imgSourcePath, String imgGoalPath){
        OutputStream os = null;
        try {
            os = new FileOutputStream(imgGoalPath);
            BufferedImage img = ImageIO.read(new FileInputStream(imgSourcePath));
            Thumbnails.of(imgSourcePath).size(img.getWidth(),img.getHeight()).toOutputStream(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return os;
    }

    /**
     * 压缩并输出到OutputStream
     * @param imgSourcePath 图片源路径
     * @param imgGoalPath 图片目标路径
     * @param width 指定图片宽
     * @param height 指定图片高
     * @return
     * @
     */
    public static OutputStream getCompressImageOutputStream(String imgSourcePath, String imgGoalPath,int width,int height){
        OutputStream os = null;
        try {
            os = new FileOutputStream(imgGoalPath);
            Thumbnails.of(imgSourcePath).size(width,height).toOutputStream(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return os;
    }

//    public void toBuffered(String imgSourcePath, String imgGoalPath,String format){
//        BufferedImage thumbnail = Thumbnails.of(imgSourcePath).asBufferedImage();
//        ImageIO.write(thumbnail,format,new File(imgGoalPath));
//    }

//----------------------------------------------------

    /**
     * 指定大小压缩图片并返回byte[]
     * 以原值/指定值 最大 的比为标准
     * @param imgSourcePath 图片源路径
     * @param width 图片的宽
     * @param height 图片的高
     * @
     */
    public static byte[] getCompressImageByte(String imgSourcePath,int width,int height) {
        byte[] b = null;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Thumbnails.of(new FileInputStream(imgSourcePath)).size(width,height).toOutputStream(os);
             b = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        close(null,os);
        return b;
    }

    /**
     * 按比例压缩图片并返回byte[]
     * @param imgSourcePath 图片源路径
     * @param scale 压缩图片比（0-1）
     * @return
     * @
     */
    public static byte[] getCompressImageByte(String imgSourcePath,float scale) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] b = new byte[0];
        try {
            Thumbnails.of(new FileInputStream(imgSourcePath)).scale(scale).toOutputStream(os);
            b = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        close(null,os);
        return b;
    }

    /**
     * 压缩图片并返回byte[]
     * @param b 传入要压缩图片的字节数据
     * @param width 指定的图片的宽
     * @param height 指定图片的高
     * @
     */
    public static byte[] getCompressImageByte(byte[] b ,int width ,int height) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            //这里还有一种写法，使用Thumbnailator.createThumbnail
            Thumbnails.of(new ByteArrayInputStream(b)).size(width,height).toOutputStream(bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] newB = bos.toByteArray();
        close(null,bos);
        return newB;
    }

    /**
     * 传入字节数组，和缩放比例压缩图片并返回字节数组
     * @param b
     * @param scale
     * @return
     */
    public static byte[] getCompressImageByte(byte[] b, float scale) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] bytes = null;
        try {
            Thumbnails.of(new ByteArrayInputStream(b)).scale(scale).toOutputStream(bos);
            bytes = bos.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
        }
        close(null,bos);
        return bytes;
    }
    /**
     * 关闭流操作
     * @param is
     * @param os
     */
    private static void close(InputStream is,OutputStream os){
        try {
            if(is != null)
                is.close();
            if(os != null)
                os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
